package com.kfd.util;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;


/**
 * 短信工具类
 */
public class SendSms {
    //腾讯云API密钥ID
    static String SECRET_ID = "AKIDlIciZCajdHemeGcjdKmzxgDYfZFhusb5";

    //腾讯云API密钥KEY
    static String SECRET_KEY = "HJ6e082h8LJ2XnqfKkvrY0DBuP0RdbKo";

    //腾讯云API
    static String URL = "sms.tencentcloudapi.com";

    //腾讯云短信模板ID
    static String TEMPLATE_ID = "1154321";

    //腾讯云应用ID SDK_APP_ID
    static String SMS_SDK_APP_ID = "1400581002";

    static String SIGN_NAME = "曲径通幽处个人网站";
    /**
     * 短信
     * @param phone 手机号
     * @param code  验证码
     * @return  是否登陆成功
     */
    public static boolean send(String phone,String code) {

        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(URL);
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            //用户手机号
            String[] phoneNumberSet1 = {phone};

            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppId(SMS_SDK_APP_ID);
            req.setSignName(SIGN_NAME);
            req.setTemplateId(TEMPLATE_ID);

            String[] templateParamSet1 = {code};
            req.setTemplateParamSet(templateParamSet1);

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(resp));
            if("Ok".equals(resp.getSendStatusSet()[0].getCode())){
                return true;
            }
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return false;
    }
}
